package com.zzx.graduation.service;


import com.zzx.graduation.entity.User;
import com.zzx.graduation.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
public class UserCRUD {

    @Autowired
    private UserRepository userRepository  ;

    public User loginByName(String username,String password){
        User user = userRepository.findByUsernameAndPassword(username,password);
        System.out.println("loginbyname查询模块结果"+user);
        if(user != null)
        {
            return user;
        }
        System.out.println("登录失败");
        return null;
    }

    public List<User> findByName(String name){
        List<User>  users = userRepository.findByStaffName(name);
        if(users != null)
        {
            return users;
        }
        System.out.println("select 失败");
        return null;
    }

    public List<User> findUsers(){
        List<User>  users = userRepository.findAll();
        if(users != null)
        {
            return users;
        }
        System.out.println("select 失败");
        return null;
    }

    public User findById(Integer id) {
        User user = userRepository.findById(id).get();
        return user;
    }

    public User addUser(User user){
        User save = userRepository.save(user);
        return user;
    }

    public boolean deleteUserById(Integer id) {
        userRepository.deleteById(id);
        return true;
    }
    public User modifyUser(User user) {
        User save = userRepository.save(user);
        return save;
    }

}
